#ifndef _MOVIE_H
#define _MOVIE_H

#include <stdio.h>

#include <sys/types.h>

#include <dirent.h>
#include <string.h>
#include <math.h>
#include "draw.h"
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void draw_pic(char *, int, int);
void draw_movie(char *, int, int, int, int, int, int *);
void draw_music_gif(char *, int, int, int, int, int);
void draw(int *);
void *play_movie(void *);

int fd;
int *plcd;

void draw(int *a)
{
    //play the movie audio
    system("madplay -o wav:- /mnt/hgfs/movie/1.mp3 | aplay &");
    // make thread to play the movie frame
    pthread_t t1;
    void *b = a;
    int err = pthread_create(&t1, NULL, play_movie, b);
}


void *play_movie(void *a)
{
    // play these bmps of movie under this dir
    draw_movie("/mnt/hgfs/movie/1/", 30, 50, -1, -1, -1, a);
}

void draw_movie(char *dirpath, int x, int y, int r, int g, int b, int *flag)
{
    fd = open("/dev/ubuntu_lcd", O_RDWR);
    plcd = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    DIR *dir;
    struct dirent *ptr;
    dir = opendir(dirpath);

    int count = 0;
    while ((ptr = readdir(dir)) != NULL)
    {
        count += 1;    //find the quantity of  bmps
    }
    closedir(dir);
    count -= 2;

    for (int i = 1; i < count; i++)
    {
        int *play = flag;
        if (*play != 2)
        {
            // back event ,stop play and return
            return;
        }

        char buffer[80];
        sprintf(buffer, "%s%d", dirpath, i);
        strcat(buffer, ".bmp");
        draw_music_gif(buffer, x, y, r, g, b);
      
    }

    munmap(plcd, 800 * 480 * 4);
    close(fd);
}

void draw_music_gif(char *name, int x, int y, int r, int g, int b)
{

    display_bmp(name, x, y, r, g, b);
    usleep(1000000 / 13.533);
}

void draw_pic(char *pathname, int x0, int y0)
{
    int fd1 = open(pathname, O_RDWR);

    if (-1 == fd1)
    {
        perror("open error");
        // return -1;
    }
    //读取bmp文件头的数据
    unsigned char buf[4] = {0};
    read(fd1, buf, 2);
    //判断是否是bmp图片
    if (buf[0] != 'B' || buf[1] != 'M')
    {
        printf("is not bmp\n");
    }

    int size, width, height, depth;
    read(fd1, buf, 4);
    size = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    lseek(fd1, 0x12, SEEK_SET);
    read(fd1, buf, 4);
    width = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    read(fd1, buf, 4);
    height = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    lseek(fd1, 0x1c, SEEK_SET);
    read(fd1, buf, 2);
    depth = buf[1] << 8 | buf[0];
    //	printf("size=%d,width=%d,height=%d,depth=%d\n",size,width,height,depth);
    //求出bmp图片每一行的字节数=宽度*（depth/8）
    int bytes_line = fabs(width) * (depth / 8);
    //满足每行的字节数是4的倍数，如果不是，则需补充赖子
    int laizi = 0;
    if (bytes_line % 4 != 0)
    {
        laizi = 4 - bytes_line % 4;
    }
    //每一行的实际字节数
    int line = bytes_line + laizi;
    //实际开辟的空间=line*height
    int size_t = line * fabs(height);
    unsigned char *p = malloc(size_t);
    //将像素数组读取到开辟的空间中去
    lseek(fd1, 54, SEEK_SET);
    read(fd1, p, size_t);
    unsigned char a, r, g, b;
    int color;
    int i = 0;
    int x, y;
    // printf("fd %d\n",fd1);
    for (y = 0; y < fabs(height); y++)
    {
        for (x = 0; x < fabs(width); x++)
        {
            b = p[i++];
            g = p[i++];
            r = p[i++];
            // if (r == r1 && g == g1 && b == b1)
            // {
            //     continue;
            // }
            if (depth == 32)
            {
                a = p[i++];
            }
            else if (depth == 24)
            {
                a = 0;
            }

            color = (a << 24) | (r << 16) | (g << 8) | b;
            lcd_draw_point(plcd, (width > 0) ? (x0 + x) : (x0 + fabs(width) - x - 1),
                           (height > 0) ? (y0 + fabs(height) - y - 1) : (y0 + y), color);
        }
        i = i + laizi; //跳过赖子
    }
    free(p);
    close(fd1);
}

#endif